//index.js
//获取应用实例
var app = getApp()
Page({
    data: {
        ordersLength: 0,

        importordersLength: 0,
        allordersLength: 0,
        tabArr: {
            curHdIndex: 0,
            curBdIndex: 0
        },
       // currentTab: 0,
      
        begintime: '2016-11-08',
        endtime: '2017-11-08',
    },

    onLoad: function (options) {

        if (typeof options != "undefined" && typeof options.targetmemberid != "undefined") {
            console.log(options.targetmemberid);
            this.setData({ "targetmemberid": options.targetmemberid });
        } else {
            this.setData({ "targetmemberid": "" });
        }
        //参数0 为非搜索触发  1 为搜索触发
        this.getMyOrder( 0,"orders");
        this.getMyOrder(0,"importorders");
     
    },
    getMyOrder: function (e,type) {
        console.log(e);
        var $that = this;
        var memberinfo = wx.getStorageSync("memberinfo");
        console.log("函数执行");
        var keyword = '', begintime = '', endtime=''; 
        //判断是否为搜索触发
        if(e==1){
            keyword = $that.data.keyword;
            begintime = $that.data.begintime;
            endtime = $that.data.endtime;
        }

        //选择url
        var url ='';
        if (type == "orders"){
            url =  "orders";
        } else if (type == "importorders"){
            url = "importorders"
        }

        app.util.request({
            url: 'entry/wxapp/PartnerOrders',
            data: {
                "m": 'nets_haojk',
                targetmemberid: $that.data.targetmemberid,
                memberid: memberinfo.memberid,
                keyword:keyword,
                begintime: begintime,
                endtime:endtime,
                uri:url,  


               
            },
            method: 'post',
            success: function (response) {
                console.log(response);
               
                
                
                if (type == "orders") {
                    var obj = response.data.data.data;
                    var total = response.data.data.total;
                    console.log(response.data.data.total);
                    for (var i = 0; i < obj.length; i++) {
                        obj[i].orderTime = form_time(obj[i].orderTime);
                    }
                
                    $that.setData({
                       // ordersLength: obj.length,
                        orderInforArray: obj,
                        orderstotal:total,
                    });
                 
                 
                    console.log(obj);
                } else if (type == "importorders") {
                    var obj = response.data.data.data;
                    var total = response.data.data.total;
                    console.log(response.data.data.total);
                    for (var i = 0; i < obj.length; i++) {
                        obj[i].orderTime = form_time(obj[i].orderTime);
                    }
                    $that.setData({
                        //importordersLength: obj.length,
                        importordersArray: obj,
                        importorderstotal: total,
                    });
                    console.log($that.data.importorderstotal);
                
                  
                }

                var alltotal = parseInt($that.data.importorderstotal) + parseInt($that.data.orderstotal);
                $that.setData({ alltotal: alltotal});

                //int转时间戳
                function form_time(a) {
                    var myDate = new Date(parseInt(a) * 1000);
                    var datestr = "";
                    datestr = myDate.getFullYear();    //获取完整的年份(4位,1970-????)
                    datestr = datestr + "-" + (myDate.getMonth() + 1);       //获取当前月份(0-11,0代表1月)
                    datestr = datestr + "-" + myDate.getDate();        //获取当前日(1-31)
                    return datestr;
                }
            }
        })
    },
    //事件处理函数
    tabFun: function (e) {
        var $that = this;
        //获取触发事件组件的dataset属性  
        var _datasetId = e.target.dataset.id;
        var _obj = {};
        _obj.curHdIndex = _datasetId;
        _obj.curBdIndex = _datasetId;
        this.setData({
            tabArr: _obj
        });
        console.log(_obj.curHdIndex);
        if (_obj.curHdIndex == 0) {
          
        } else if (_obj.curHdIndex == 1) {
        
        } else if (_obj.curHdIndex == 2) {
         
        }
    },
    //事件处理函数
    bindViewTap: function () {
        wx.navigateTo({
            url: '../detail/index'
        })
    },

    //底部列表条转  
    skipToOrder: function () {
        wx.navigateTo({
            url: '../partnerOrder/index',
        })
    },
    skipToIndex: function () {
        wx.navigateTo({
            url: '../partnerIndex/index',
        })
    },
    skipToMember: function () {
        wx.navigateTo({
            url: '../partnerMember/index',
        })
    },
    skipToFinance: function () {
        wx.navigateTo({
            url: '../partnerFinance/index',
        })
    },
    bindKeyInput: function (e) {
        var value = e.detail.value;
       

        this.setData({
            keyword: value
        });
       


    },

    //  点击日期组件确定事件  
    bindDateChange: function (e) {
        this.setData({
            begintime: e.detail.value
        })
    },
    //  点击日期组件确定事件  
    bindDateChange2: function (e) {
        this.setData({
            endtime: e.detail.value
        })
    },

    //点击搜索
    search:function(e){
         console.log(e);
         if (e.currentTarget.dataset.type==1){
             this.getMyOrder('1', "orders");
             console.log("已审")
         } else if (e.currentTarget.dataset.type == 0){
             this.getMyOrder('1', "importorders");
             console.log("未审")
         }
       
        
    },

    

})
